package com.adderis.soft.db;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.database.QueryDataSet;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.xml.FlatXmlDataSet;

/**
 * @author Reda
 * 
 */
public class DataExtractor {

	public static void main(String[] args) throws ClassNotFoundException,
			SQLException, DataSetException, FileNotFoundException, IOException {
		Class.forName("com.mysql.jdbc.Driver");

		Connection jdbcConnection = DriverManager.getConnection(
				"jdbc:mysql://localhost:3306/adderis", "root", "");
		IDatabaseConnection connection = null;
		try {
			connection = new DatabaseConnection(jdbcConnection);
		} catch (DatabaseUnitException e) {
			e.printStackTrace();
		}

		QueryDataSet partialDataSet = new QueryDataSet(connection);

		partialDataSet.addTable("nomcommercial");

		// XML file into which data needs to be extracted
		FlatXmlDataSet.write(partialDataSet, new FileOutputStream(
				"src/test/resources/test-dataset_temp.xml"));
		System.out.println("Dataset written");
	}
}

